﻿<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  
  xmlns:local="clr-namespace:System.Controls">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Common.xaml"/>
  </ResourceDictionary.MergedDictionaries>

  <ControlTemplate x:Key="controlTemplate" TargetType="{x:Type local:CheckBox}">

    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="leftColumn"  Width="Auto"/>
        <ColumnDefinition x:Name="rightColumn" />
      </Grid.ColumnDefinitions>

      <Border
        x:Name="check_border"
        Grid.Column="0"
        Width="15"
        Height="15"
  		  CornerRadius="2" 
  		  BorderThickness="0,1"
  		  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
  		  BorderBrush="{TemplateBinding BorderBrush}" 
  		  Background="{TemplateBinding Background}" >

        <Rectangle 
          x:Name="check"
          Height="12" 
          Width="10" 
          RenderTransformOrigin="0.5,0.5"
          VerticalAlignment="Center">
          <Rectangle.Fill>
            <DrawingBrush Stretch="Fill" >
              <DrawingBrush.Drawing>
                <GeometryDrawing Brush="{StaticResource EmptyCheckMarkFill}"  Geometry="{StaticResource checkmark}"/>
              </DrawingBrush.Drawing>
            </DrawingBrush>
          </Rectangle.Fill>

          <Rectangle.RenderTransform>
            <TransformGroup>
              <ScaleTransform ScaleX="1.3" ScaleY="1.35"/>
              <RotateTransform Angle="12"/>
              <TranslateTransform X="1" Y="-1.5" />
            </TransformGroup>
          </Rectangle.RenderTransform>
          <Rectangle.Effect>
            <DropShadowEffect BlurRadius="5" Color="Black" ShadowDepth="3" Direction="330"/>
          </Rectangle.Effect>
        </Rectangle>

      </Border>

      <TextBlock
		    x:Name="textBlock"
		    Grid.Column="1"
        Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HasContent, Converter={StaticResource b2v}}"
		    Text="{TemplateBinding Content}"
		    Margin="{TemplateBinding Padding}" 
		    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
		    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
		    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
		    TextWrapping="{TemplateBinding TextWrapping}" 
        MaxWidth="{TemplateBinding TextWidth}"/>

    </Grid>

    <ControlTemplate.Triggers>
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="CheckAlign" Value="LEFT"/>
          <Condition Property="HasContent" Value="True"/>
        </MultiTrigger.Conditions>
        <Setter Property="Padding" Value="4,0,0,0"/>
        <Setter Property="SharedSizeGroup" Value="{Binding SharedSizeGroup, RelativeSource={RelativeSource TemplatedParent}}" TargetName="rightColumn"/>
        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="leftColumn" />
        <Setter Property="Width" Value="Auto" TargetName="leftColumn"/>
        <Setter Property="Width" Value="*" TargetName="rightColumn" />
        <Setter Property="Grid.Column" Value="0"   TargetName="check_border"/>
        <Setter Property="Grid.Column" Value="1"   TargetName="textBlock"/>
      </MultiTrigger>

      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="CheckAlign" Value="RIGHT"/>
          <Condition Property="HasContent" Value="True"/>
        </MultiTrigger.Conditions>
        <Setter Property="Padding" Value="0,0,4,0"/>
        <Setter Property="Grid.Column" Value="1"   TargetName="check_border"/>
        <Setter Property="Grid.Column" Value="0"   TargetName="textBlock"/>

        <Setter Property="Width" Value="Auto" TargetName="rightColumn" />
        <Setter Property="Width" Value="*"    TargetName="leftColumn"/>

        <Setter Property="SharedSizeGroup" Value="{x:Null}" TargetName="rightColumn" />
        <Setter Property="SharedSizeGroup" Value="{Binding SharedSizeGroup, RelativeSource={RelativeSource TemplatedParent}}" TargetName="leftColumn"/>
      </MultiTrigger>


      <Trigger Property="IsChecked" Value="True">
        <Setter Property="Fill" TargetName="check">
          <Setter.Value>
            <DrawingBrush Stretch="Fill" >
              <DrawingBrush.Drawing>
                <GeometryDrawing Brush="{StaticResource CheckMarkFill}"  Geometry="{StaticResource checkmark}"/>
              </DrawingBrush.Drawing>
            </DrawingBrush>
          </Setter.Value>
        </Setter>
      </Trigger>

      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="IsChecked" Value="{x:Null}"/>
          <Condition Property="IsThreeState" Value="True"/>
        </MultiTrigger.Conditions>
        <Setter Property="Fill" TargetName="check" Value="{x:Null}"/>
      </MultiTrigger>
      
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="IsChecked" Value="{x:Null}"/>
          <Condition Property="IsThreeState" Value="False"/>
        </MultiTrigger.Conditions>
        <Setter Property="Fill" TargetName="check" Value="{x:Null}"/>
      </MultiTrigger>


    </ControlTemplate.Triggers>

  </ControlTemplate>


  <Style TargetType="{x:Type local:CheckBox}" BasedOn="{StaticResource {x:Type Control}}">

    <Setter Property="TextWrapping" Value="Wrap"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualCheck}"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxBorder}"/>
    <Setter Property="Background" Value="{StaticResource CheckBoxBackground}"/>
    <Setter Property="Template" Value="{StaticResource controlTemplate}"/>
    
  </Style>


</ResourceDictionary>